﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Reflection.Emit;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace AutoWrapper
{
    public static class RESTWrapper
    {
        public static void OpenService(Type serviceType,Uri baseAddress)
        {
            ServiceHost sh = new ServiceHost(serviceType, baseAddress);

            WebHttpBinding binding = new WebHttpBinding();

            ServiceEndpoint endpoint = sh.AddServiceEndpoint(serviceType, binding, serviceType.Name);

            sh.Open();
        }
        public static Type GetServiceInterface(ModuleBuilder serviceModuleBuilder, Type serviceClass)
        {

            return null;
        }
        public static Type GetDecoratedServiceType(ModuleBuilder serviceModuleBuilder,Type serviceClass)
        {
            String typeName = serviceClass.Name;

            TypeBuilder serviceTypeBuilder = serviceModuleBuilder.DefineType(typeName, TypeAttributes.Public);

            //set the ServiceContract attribute of the type
            serviceTypeBuilder.SetCustomAttribute(ServiceAttributeBuilders.ServiceContractAttributeBuilder());


            foreach (MethodInfo mi in serviceClass.GetMethods())
            {
                // decorate the origin method to with the service attributes such as the OperationContract
                // 
                MethodServiceAttributeDecorators.DecorateMethod(serviceTypeBuilder, mi);
            }


            Type builtServiceType = serviceTypeBuilder.CreateType();
            return builtServiceType;
        }
    }
}
